/*
  ==============================================================================

  This is an automatically generated file created by the Jucer!

  Creation date:  27 Jul 2008 12:24:04 pm

  Be careful when adding custom code to these files, as only the code within
  the "//[xyz]" and "//[/xyz]" sections will be retained when the file is loaded
  and re-saved.

  Jucer version: 1.11

  ------------------------------------------------------------------------------

  The Jucer is part of the JUCE library - "Jules' Utility Class Extensions"
  Copyright 2004-6 by Raw Material Software ltd.

  ==============================================================================
*/

#include "AudioSpecMeterEditor.h"
#include "AudioSpecMeterPlugin.h"


//==============================================================================
AudioSpecMeterEditor::AudioSpecMeterEditor (AudioSpecMeterPlugin* owner_)
    : PluginEditorComponent (owner_),
      owner (owner_)
{
    Lpeak = Lmin = Lrms = Rpeak = Rmin = Rrms = Corr = 0.0f;
    xtimer = -1;

    for (long i = 0; i < 16; i++)
        band[0][i] = band[1][i] = 0.0f;

    backgroundCachedImage = ImageCache::getFromMemory (specmeter_png, specmeter_pngSize);

	startTimer (1000 / 20); // 50fps
}

AudioSpecMeterEditor::~AudioSpecMeterEditor()
{
    stopTimer ();

    ImageCache::release (backgroundCachedImage);
}

//==============================================================================
void AudioSpecMeterEditor::paint (Graphics& g)
{
    long r, p;

    g.drawImageAt (backgroundCachedImage, 0, 0);

/*
    pContext->setFillColor(kGreenCColor);

    p = x2pix(Lmin);
    block(p - 3, 10, p - 1, 18);
    pContext->fillRect(block);

    p = x2pix(Rmin);
    block(p - 3, 18, p - 1, 26);
    pContext->fillRect(block);
*/

    // left peak and rms
    r = x22pix (Lrms);
    p = x2pix (Lpeak);
    if (p < 453) {
        g.setColour (Colours::black.withAlpha (0.85f));
        g.fillRect (p - 1, 11, 478 - (p - 1), 7);
    }
    g.setColour (Colours::white.withAlpha (0.5f));
    g.fillRect (2, 11, r - 2, 7);
//    g.fillRect (r - 1, 10, p - (r - 1), 8);

    // right peak and rms
    r = x22pix (Rrms);
    p = x2pix (Rpeak);
    if (p < 453) {
        g.setColour (Colours::black.withAlpha (0.85f));
        g.fillRect (p - 1, 19, 478 - (p - 1), 7);
    }
    g.setColour (Colours::white.withAlpha (0.5f));
    g.fillRect (2, 19, r - 2, 7);
//    g.fillRect (r - 1, 18, p - (r - 1), 8);

    // correlation meter
    g.setColour (Colours::black.withAlpha (0.85f));
    g.fillRect (235, 42, 10, jmin (134 - (int)(90 * Corr), 95));

    // octave bands
    float dB;
    int x1 = 2, x2 = 256, height = 0;
    for (int i = 0; i < 13; i++)
    {    
        dB = band[0][i];
        height = 12 - (int)(20.0 * log (dB + 0.0000000000000000001));
        g.fillRect (x1, 42, 18, jmax (0, height));
        x1 += 17;

        dB = band[1][i];
        height = 12 - (int)(20.0 * log (dB + 0.0000000000000000001));
        g.fillRect (x2, 42, 18, jmax (0, height));        x2 += 17;
    }
}

//==============================================================================
void AudioSpecMeterEditor::timerCallback ()
{
/*
	int xnow = owner->counter;
	if (xnow != xtimer)
	{
	    xtimer = xnow;
*/
	  
    	Lpeak = owner->Lpeak;
	    Lmin  = owner->Lmin;
	    Lrms  = owner->Lrms;
	    Rpeak = owner->Rpeak;
	    Rmin  = owner->Rmin;
	    Rrms  = owner->Rrms;
	    Corr  = owner->Corr;

        for (int i = 0; i < 13; i++)
        {
            band[0][i] = owner->band[0][i];
            band[1][i] = owner->band[1][i];
        }

        repaint ();
/*
	}
*/
}

//==============================================================================
void AudioSpecMeterEditor::updateParameters ()
{
}

//==============================================================================
int AudioSpecMeterEditor::x2pix (float x)
{
  float dB = x;
  int p = 478;
  
  if(x > 0.00000005f) dB = 8.6858896f * (float)log(x); else dB = -146.0f;
  if(dB < -20.0) 
    p = 293 + (int)(2.0f * dB); 
  else 
    p = 453 + (int)(10.0f * dB);
  
  return p;
}

//==============================================================================
int AudioSpecMeterEditor::x22pix (float x) //power version for squared summed
{
  float dB = x;
  int p = 478;
  
  if(x > 0.00000005f) dB = 4.3429448f * (float)log(x); else dB = -146.0f;
  if(dB < -20.0) 
    p = 293 + (int)(2.0f * dB); 
  else 
    if(dB < 0.0f) p = 453 + (int)(10.0f * dB);
  
  return p;
}

//==============================================================================
static const unsigned char resource_AudioSpecMeterEditor_specmeter_png[] = { 137,80,78,71,13,10,26,10,0,0,0,13,73,72,68,82,0,0,1,224,0,0,0,150,8,2,0,0,0,137,186,204,170,0,0,0,1,115,82,71,66,0,174,206,28,
233,0,0,0,6,98,75,71,68,0,255,0,255,0,255,160,189,167,147,0,0,0,9,112,72,89,115,0,0,11,19,0,0,11,19,1,0,154,156,24,0,0,0,7,116,73,77,69,7,216,8,15,18,16,53,89,121,206,25,0,0,14,229,73,68,65,84,120,218,
237,157,95,136,29,87,29,199,127,115,55,217,59,119,75,112,87,139,36,15,42,217,10,101,209,135,166,32,186,121,146,32,62,172,85,161,121,40,164,32,66,66,193,38,197,210,230,73,35,69,172,62,37,165,98,82,177,
36,80,44,93,16,49,32,226,62,20,9,62,101,21,193,245,65,189,47,118,23,90,36,139,26,179,125,200,222,217,77,246,142,15,179,123,156,156,59,51,123,230,207,153,61,39,251,249,16,14,103,102,206,247,119,206,204,
239,55,231,254,246,220,153,92,17,0,0,0,0,0,168,200,225,87,227,195,175,198,218,158,130,77,67,59,187,154,109,193,78,129,145,246,237,128,141,112,77,54,213,63,109,103,83,110,42,238,49,111,12,68,8,52,16,238,
163,149,10,145,173,217,169,28,166,13,218,201,60,181,10,231,213,136,29,176,55,65,23,79,166,54,38,232,226,79,104,45,116,137,16,40,75,71,213,86,47,4,141,88,212,236,84,54,107,105,60,205,218,105,202,56,84,
152,40,87,47,4,171,23,2,195,41,15,79,129,143,28,200,140,123,85,169,252,129,175,236,100,110,86,182,83,127,146,77,206,168,190,205,166,236,128,141,191,2,19,191,164,3,216,146,167,70,237,167,111,25,194,3,154,
156,160,181,217,240,232,197,120,16,233,51,181,201,196,84,48,59,215,177,83,176,0,98,120,39,104,31,63,149,237,168,150,245,237,64,35,171,13,121,19,98,58,240,202,134,177,73,200,21,244,216,120,122,1,251,122,
137,67,11,166,213,11,193,202,249,64,11,196,94,184,123,94,80,156,59,87,182,147,121,27,152,216,49,153,103,221,177,3,165,214,55,138,87,57,242,102,240,106,110,194,197,208,62,65,102,52,107,121,71,169,140,64,
179,99,98,214,196,78,102,214,99,152,161,140,166,240,154,170,101,59,208,200,4,173,109,106,211,177,246,151,77,83,137,109,230,159,131,153,209,158,185,238,65,120,0,0,0,0,0,0,88,99,251,15,174,175,189,255,76,
82,233,172,111,12,39,186,234,48,155,108,62,124,155,31,255,196,225,127,125,176,90,77,126,232,241,233,15,63,248,167,141,129,61,250,248,167,42,143,138,49,59,50,230,183,131,215,226,153,201,6,230,229,254,218,
182,217,116,7,34,50,156,232,110,174,223,99,147,205,135,123,83,68,146,202,112,162,155,28,50,220,236,202,150,50,88,86,91,188,185,33,99,201,158,196,114,131,167,172,234,141,91,102,204,218,152,211,44,222,90,
11,250,107,65,127,109,241,214,154,154,115,85,153,252,75,215,147,102,26,7,210,159,6,155,235,247,66,25,142,79,116,55,215,239,141,143,108,118,214,55,34,233,140,79,28,204,108,204,81,142,250,114,52,185,69,
85,132,15,183,219,31,220,105,223,45,56,42,34,161,12,135,34,145,116,198,119,202,80,134,209,250,189,241,137,131,233,91,107,219,218,78,251,205,245,123,97,225,209,174,108,253,91,58,227,35,183,104,210,123,
186,211,206,250,134,74,173,180,163,225,131,105,221,230,131,163,74,247,152,215,184,218,209,7,175,67,3,110,242,122,204,9,199,215,36,201,166,131,254,90,124,68,119,107,58,209,46,104,214,73,159,76,40,195,72,
58,234,227,101,124,226,96,122,51,146,78,40,195,228,67,99,180,177,250,208,83,59,85,179,68,50,62,113,240,254,237,251,234,170,97,25,203,123,104,57,211,90,114,203,21,143,100,67,198,210,93,36,229,112,162,171,
13,82,229,233,233,177,13,39,186,247,111,223,87,18,237,232,134,140,153,24,209,198,185,235,128,51,47,75,217,43,153,119,52,239,130,215,119,98,230,152,71,205,154,71,78,166,247,109,140,57,143,151,195,255,151,
233,69,12,149,68,223,204,90,26,233,140,238,26,78,116,59,131,32,61,172,209,15,7,181,95,149,218,40,51,27,12,123,113,122,57,101,180,89,102,47,153,231,143,101,44,87,182,92,32,217,92,191,55,222,219,42,24,73,
166,246,254,237,251,201,93,163,149,218,41,104,131,41,62,193,226,43,208,89,223,24,239,109,153,92,186,130,41,163,224,254,213,26,36,243,215,232,81,201,204,247,123,91,153,25,101,222,233,36,198,55,7,99,233,
50,207,59,121,215,161,96,144,233,163,154,101,123,99,30,229,100,40,65,127,237,100,152,157,65,191,28,202,241,181,252,9,122,251,252,7,99,219,103,146,248,126,16,136,72,103,16,36,110,206,219,220,62,201,193,
152,178,16,73,71,219,204,107,92,188,153,30,149,102,10,203,88,174,105,57,189,63,111,51,47,218,181,198,195,94,156,220,53,90,105,216,139,218,44,219,62,243,238,203,187,116,187,94,225,130,43,153,119,58,197,
87,219,100,83,25,215,202,244,164,164,249,58,243,44,210,73,125,166,239,148,143,210,87,163,108,212,169,205,188,49,167,185,57,249,64,106,60,123,100,82,149,42,107,86,141,47,30,205,254,106,49,181,196,49,24,
27,239,109,109,14,198,134,189,56,25,80,82,25,239,109,169,235,152,28,85,103,21,73,71,53,222,94,211,233,109,37,167,161,153,50,57,58,218,88,235,183,184,49,150,177,108,104,89,228,110,102,156,143,14,96,180,
163,76,173,214,87,178,115,216,139,147,9,66,37,95,197,167,41,114,183,84,251,188,51,29,109,163,141,185,148,86,205,77,234,164,210,39,46,114,183,206,12,144,233,71,101,89,77,74,106,170,29,189,62,73,169,140,
164,91,106,198,85,167,233,235,92,234,242,22,251,90,141,89,49,123,100,50,158,153,140,103,38,213,164,172,178,230,100,127,178,153,222,201,99,133,0,0,190,61,7,61,183,116,146,107,1,251,132,143,61,241,232,237,
191,252,167,125,45,150,31,122,203,111,7,63,179,245,28,52,0,0,52,69,147,207,65,3,236,31,34,217,168,35,239,68,27,195,176,107,99,96,88,110,202,71,237,91,238,68,186,217,134,159,131,78,58,160,164,220,7,229,
35,149,229,145,108,12,195,174,141,129,97,57,93,218,155,148,44,89,30,134,221,226,169,191,214,115,208,73,7,148,148,251,161,20,185,91,89,110,53,181,183,116,202,158,142,217,71,203,5,84,123,14,122,251,75,194,
167,22,159,226,47,95,216,39,244,190,240,232,224,15,85,190,38,26,134,221,71,158,56,84,77,107,111,84,140,217,29,203,191,12,222,82,51,239,226,173,181,100,206,189,57,185,253,248,115,208,95,83,171,25,106,106,
78,239,84,90,213,224,128,214,135,237,207,91,0,127,233,68,27,34,135,24,179,191,99,110,211,242,236,145,73,109,77,57,243,145,103,158,131,6,0,0,0,0,104,156,229,229,101,74,74,74,74,202,250,101,125,170,76,223,
28,229,40,71,57,202,209,106,71,171,209,192,155,132,211,211,211,104,209,62,52,90,0,119,224,85,111,128,150,62,24,176,188,159,45,3,0,0,0,0,192,30,146,94,240,214,234,187,174,133,171,6,90,99,219,90,77,94,89,
88,173,223,10,170,154,218,209,175,125,189,232,183,66,68,237,237,117,174,22,81,101,45,87,255,18,127,47,198,108,213,44,150,75,244,164,213,71,111,176,60,173,214,216,182,86,147,87,22,86,235,183,130,170,190,
118,244,14,111,167,95,173,77,217,49,151,141,40,23,174,115,217,136,170,112,175,53,126,111,219,27,115,53,191,99,185,2,217,95,18,54,178,202,94,193,72,29,109,29,185,215,107,255,203,203,203,237,63,240,208,
94,178,208,116,88,238,137,220,208,114,179,87,181,230,221,228,2,141,143,220,187,75,113,192,228,230,175,19,52,117,166,143,154,83,79,155,97,84,249,18,41,109,251,103,90,167,223,68,85,193,65,190,71,148,85,
127,85,190,170,123,114,55,213,244,163,189,224,124,104,38,22,49,121,204,174,230,223,95,149,189,232,209,69,76,78,179,114,42,154,104,219,191,74,149,251,173,233,23,175,35,170,166,191,246,42,185,179,116,55,
85,14,123,123,193,185,231,31,45,173,78,208,202,1,21,220,176,87,89,210,158,44,50,200,126,162,206,249,18,81,237,71,145,189,148,124,95,133,174,187,103,210,254,83,28,149,159,19,216,87,79,113,72,214,247,117,
126,61,17,33,30,62,121,98,233,41,14,241,234,249,16,75,3,198,50,0,0,0,0,0,0,0,0,0,0,64,75,4,170,22,127,250,112,57,229,63,86,145,32,177,45,49,105,118,248,213,56,115,255,234,133,192,68,142,83,144,120,26,
249,0,0,224,66,6,253,217,157,57,62,18,9,83,77,114,54,131,191,174,34,65,98,91,210,70,6,141,83,144,248,25,249,0,0,224,66,6,253,185,163,229,148,127,90,65,130,196,182,196,164,217,209,139,241,32,146,94,40,
34,146,174,152,102,208,56,5,137,159,145,15,0,0,100,208,72,144,212,200,160,51,247,175,156,39,131,70,66,6,13,0,0,118,51,232,47,151,156,227,223,93,65,130,196,182,164,141,12,26,167,32,241,51,242,1,0,192,133,
12,250,235,51,219,95,129,155,149,193,187,43,72,144,216,150,164,131,245,230,205,155,73,229,248,241,227,90,6,29,138,68,34,34,146,174,244,31,204,160,243,228,56,5,137,227,145,15,0,0,142,103,208,153,121,72,
78,37,248,117,31,9,18,219,18,147,32,158,201,89,131,238,27,174,65,227,20,36,126,70,62,0,0,184,144,65,63,51,83,78,249,139,62,18,36,182,37,109,100,208,56,5,137,159,145,15,0,0,46,100,208,223,156,145,137,80,
214,35,17,49,169,4,111,245,145,32,177,45,49,204,160,195,80,162,104,123,9,79,213,251,231,204,50,104,156,130,196,207,200,7,0,0,23,50,232,231,143,149,83,190,177,132,4,137,109,137,73,179,99,151,227,104,39,
113,78,151,166,107,208,56,5,137,159,145,15,0,0,46,100,208,47,149,156,227,95,91,66,130,196,182,36,189,153,247,42,224,177,203,217,79,113,44,157,51,123,147,16,167,32,113,59,242,1,0,192,229,12,250,59,37,231,
248,31,45,33,65,98,91,98,210,204,48,131,206,3,167,32,241,52,242,1,0,192,133,12,250,7,37,231,248,239,45,33,65,98,91,98,152,65,103,254,175,6,139,134,25,52,78,65,226,103,228,3,0,128,11,25,244,197,217,114,
202,243,139,72,144,216,150,152,52,155,205,89,131,54,205,160,113,10,18,63,35,31,0,0,92,200,160,47,239,204,241,201,255,105,160,200,217,12,206,45,34,65,98,91,210,70,6,141,83,144,248,25,249,0,0,224,66,6,125,
181,228,28,127,102,17,9,18,219,146,116,176,230,189,10,56,123,57,78,126,206,45,121,134,67,213,23,207,152,189,73,136,83,144,184,29,249,0,0,224,114,6,61,127,162,156,242,212,13,36,72,108,75,76,154,157,184,
26,103,254,62,178,233,26,52,78,65,226,103,228,3,0,128,11,25,244,245,19,178,179,138,103,82,6,167,110,32,65,98,91,98,152,65,39,149,36,119,86,220,56,99,150,65,227,20,36,126,70,62,0,0,184,144,65,47,148,156,
227,159,190,129,4,137,109,73,169,12,90,195,52,131,198,41,72,252,140,124,0,0,112,33,131,254,253,92,57,229,23,23,144,32,177,45,49,105,54,119,53,206,204,69,76,51,104,156,130,196,207,200,7,0,0,71,50,232,158,
200,64,68,196,164,178,253,177,128,4,137,77,73,58,88,243,94,5,156,219,89,131,214,94,203,90,48,124,147,16,167,32,113,59,242,1,0,192,229,12,250,143,115,209,35,97,120,55,18,17,147,74,240,249,5,36,72,108,75,
12,215,160,147,236,35,26,72,152,74,67,22,12,215,160,113,10,18,63,35,31,0,0,28,200,160,239,252,237,233,82,202,169,207,92,71,130,196,182,196,164,89,205,231,160,113,10,18,79,35,31,0,0,92,200,160,223,43,57,
199,63,118,29,9,18,219,146,54,50,104,156,130,196,207,200,7,0,0,71,50,232,143,134,242,223,200,176,156,154,154,71,130,196,182,164,165,12,26,167,32,241,48,242,1,0,192,133,12,250,206,169,114,171,36,83,243,
72,144,216,150,164,55,119,125,147,80,195,240,77,66,156,130,196,241,200,7,0,0,135,51,232,91,37,231,248,35,83,243,72,144,216,150,152,52,155,123,39,150,193,206,11,132,169,210,240,77,66,156,130,196,211,200,
7,0,0,7,50,232,247,238,156,46,165,124,108,234,26,18,36,182,37,38,205,158,126,39,123,13,250,250,179,70,25,52,78,65,226,105,228,3,0,128,3,25,244,223,7,167,35,145,143,68,242,97,104,84,62,217,187,134,4,137,
109,73,11,25,52,78,65,226,105,228,3,0,128,3,25,244,159,7,167,69,100,93,194,9,137,76,42,79,246,174,33,65,98,91,98,18,196,167,114,50,232,121,179,12,26,167,32,241,52,242,1,0,192,129,12,250,119,131,179,201,
111,33,203,206,143,34,23,87,190,212,187,130,4,137,109,73,58,88,243,94,5,60,245,78,156,105,101,254,164,209,155,132,56,5,137,227,145,15,0,0,14,103,208,191,25,156,45,243,179,179,242,213,222,21,36,72,108,
75,76,130,248,244,175,98,17,25,68,210,219,249,77,239,164,126,237,164,209,26,52,78,65,226,105,228,3,0,128,27,25,180,136,220,145,112,106,123,25,100,151,122,242,177,128,4,137,85,137,121,6,61,138,121,6,141,
83,144,248,24,249,0,0,224,64,6,253,243,193,203,165,148,223,232,93,66,130,196,182,164,133,12,26,167,32,241,52,242,1,0,192,129,12,250,205,146,115,252,115,189,75,72,144,216,150,152,52,59,251,219,236,12,250,
202,87,140,50,104,156,130,196,211,200,7,0,0,7,50,232,215,7,223,45,165,124,177,247,67,36,72,108,75,210,155,121,175,2,26,102,208,121,114,156,130,196,241,200,7,0,0,135,51,232,239,151,92,37,121,165,119,9,
9,18,219,18,147,102,53,215,160,113,10,18,79,35,31,0,0,28,200,160,207,199,103,15,69,225,251,225,224,147,81,207,164,124,165,119,9,9,18,219,18,147,32,174,249,28,52,78,65,226,105,228,3,0,128,3,25,244,233,
248,121,17,25,72,220,147,192,164,114,45,120,3,9,18,219,18,147,32,174,249,139,42,56,5,137,167,145,15,0,0,14,100,208,167,226,111,149,82,206,7,63,69,130,196,182,196,164,89,205,95,245,198,41,72,60,141,124,
0,0,112,32,131,158,139,159,43,165,92,8,222,68,130,196,182,36,189,153,247,42,224,220,213,56,243,215,41,22,158,53,122,147,16,167,32,113,60,242,1,0,192,225,12,250,68,252,66,40,27,145,116,147,82,68,138,43,
11,193,155,74,178,107,227,164,114,35,248,9,189,208,75,169,94,76,130,248,196,213,236,53,232,27,103,140,214,160,241,35,189,120,26,249,0,0,224,64,6,61,27,159,237,73,103,32,67,17,49,169,44,6,87,148,36,93,
22,168,22,131,43,39,226,23,210,123,118,21,210,203,62,239,197,36,136,103,47,103,103,208,139,231,140,50,104,34,159,94,60,141,124,0,0,112,32,131,62,22,191,148,94,37,217,181,92,12,174,32,65,98,91,98,18,196,
199,114,50,232,37,179,12,26,167,32,241,52,242,1,0,192,129,12,122,38,126,177,148,178,31,188,142,4,137,109,137,73,179,153,139,217,25,116,255,188,81,6,141,83,144,120,26,249,0,0,224,64,6,125,52,254,118,41,
229,74,240,99,36,72,108,75,76,154,29,205,201,160,87,204,50,104,156,130,196,211,200,7,0,0,0,0,0,0,0,216,119,44,47,47,171,138,170,55,222,69,218,178,141,142,210,54,151,119,176,218,157,86,177,237,26,123,198,
71,175,158,165,0,24,237,113,212,101,5,123,136,124,34,223,187,200,111,204,187,246,70,169,89,182,209,81,94,23,246,186,107,193,169,86,123,201,52,62,122,245,108,220,15,149,247,16,249,68,190,47,145,223,105,
202,208,244,244,244,174,123,154,237,203,134,253,81,155,86,63,12,91,238,206,70,47,153,94,72,239,180,116,70,246,162,139,200,39,242,221,137,252,3,246,174,130,141,48,26,181,188,188,188,220,120,71,202,102,
82,218,232,162,184,223,198,109,106,9,145,189,211,209,28,212,218,165,203,188,9,211,195,104,103,90,39,242,137,252,102,251,234,216,24,235,244,244,180,118,105,236,89,110,188,163,61,153,83,90,152,53,218,201,
80,236,185,222,60,21,154,222,33,61,47,183,51,48,34,159,200,111,214,108,199,247,171,239,254,39,121,41,31,55,126,82,73,220,164,63,219,219,153,64,109,244,149,88,75,80,39,165,237,217,171,200,33,242,137,124,
219,103,212,88,220,248,251,93,182,246,141,127,59,95,157,231,85,124,233,101,215,239,178,125,159,16,137,124,34,159,200,7,0,0,0,0,0,0,0,0,0,128,135,141,255,1,180,51,209,192,160,116,136,244,0,0,0,0,73,69,
78,68,174,66,96,130,0,0};

const char* AudioSpecMeterEditor::specmeter_png = (const char*) resource_AudioSpecMeterEditor_specmeter_png;
const int AudioSpecMeterEditor::specmeter_pngSize = 3941;

